package org.mtree.context;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用上下文
 * 
 * @author rushman
 */
public class CommonContext implements Context {

    private final Map<String, Object> data;

    public CommonContext() {
        super();
        this.data = new HashMap<>();
    }

    public CommonContext(Map<String, ?> data) {
        super();
        this.data = new HashMap<>();
        this.data.putAll(data);
    }

    @Override
    public Object get(String name) {
        return data.get(name);
    }

    @Override
    public Object set(String name, Object value) {
        if (value != null) {
            return data.put(name, value);
        } else {
            Object val = data.get(name);
            data.remove(name);
            return val;
        }
    }

    @Override
    public Object remove(String name) {
        return data.remove(name);
    }

    @Override
    public boolean has(String name) {
        return data.containsKey(name);
    }

    @Override
    public Collection<String> nameSet() {
        return data.keySet();
    }

}
